import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

// 五子棋游戏主类
public class GobangGame extends JFrame {
    private static final int BOARD_SIZE = 15;
    private static final int CELL_SIZE = 40;
    private static final int MARGIN = 50;

    private int[][] board;
    private boolean isBlackTurn;
    private JLabel statusLabel;

    public GobangGame() {
        board = new int[BOARD_SIZE][BOARD_SIZE];
        isBlackTurn = true;

        setTitle("五子棋游戏");
        setSize(BOARD_SIZE * CELL_SIZE + MARGIN * 2, BOARD_SIZE * CELL_SIZE + MARGIN * 2 + 50);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel boardPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // 绘制棋盘网格
                for (int i = 0; i < BOARD_SIZE; i++) {
                    g.drawLine(MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (BOARD_SIZE - 1) * CELL_SIZE, MARGIN + i * CELL_SIZE);
                    g.drawLine(MARGIN + i * CELL_SIZE, MARGIN, MARGIN + i * CELL_SIZE, MARGIN + (BOARD_SIZE - 1) * CELL_SIZE);
                }
                // 绘制棋子
                for (int i = 0; i < BOARD_SIZE; i++) {
                    for (int j = 0; j < BOARD_SIZE; j++) {
                        if (board[i][j] == 1) {
                            g.setColor(Color.BLACK);
                            g.fillOval(MARGIN + j * CELL_SIZE - 15, MARGIN + i * CELL_SIZE - 15, 30, 30);
                        } else if (board[i][j] == 2) {
                            g.setColor(Color.WHITE);
                            g.fillOval(MARGIN + j * CELL_SIZE - 15, MARGIN + i * CELL_SIZE - 15, 30, 30);
                        }
                    }
                }
            }
        };
        boardPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int x = (e.getX() - MARGIN + CELL_SIZE / 2) / CELL_SIZE;
                int y = (e.getY() - MARGIN + CELL_SIZE / 2) / CELL_SIZE;
                if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[y][x] == 0) {
                    board[y][x] = isBlackTurn ? 1 : 2;
                    boardPanel.repaint();
                    if (checkWin(y, x)) {
                        JOptionPane.showMessageDialog(GobangGame.this, isBlackTurn ? "黑方获胜！" : "白方获胜！");
                        resetGame();
                    } else {
                        isBlackTurn = !isBlackTurn;
                        statusLabel.setText(isBlackTurn ? "轮到黑方落子" : "轮到白方落子");
                    }
                }
            }
        });
        add(boardPanel, BorderLayout.CENTER);

        statusLabel = new JLabel("轮到黑方落子");
        add(statusLabel, BorderLayout.SOUTH);

        JButton resetButton = new JButton("重新开始");
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resetGame();
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(resetButton);
        add(buttonPanel, BorderLayout.NORTH);

        setVisible(true);
    }

    private boolean checkWin(int row, int col) {
        int currentPlayer = isBlackTurn ? 1 : 2;
        // 检查横向
        int count = 1;
        for (int i = col - 1; i >= 0 && board[row][i] == currentPlayer; i--) {
            count++;
        }
        for (int i = col + 1; i < BOARD_SIZE && board[row][i] == currentPlayer; i++) {
            count++;
        }
        if (count >= 5) return true;

        // 检查纵向
        count = 1;
        for (int i = row - 1; i >= 0 && board[i][col] == currentPlayer; i--) {
            count++;
        }
        for (int i = row + 1; i < BOARD_SIZE && board[i][col] == currentPlayer; i++) {
            count++;
        }
        if (count >= 5) return true;

        // 检查正斜向
        count = 1;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == currentPlayer; i--, j--) {
            count++;
        }
        for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == currentPlayer; i++, j++) {
            count++;
        }
        if (count >= 5) return true;

        // 检查反斜向
        count = 1;
        for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == currentPlayer; i--, j++) {
            count++;
        }
        for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == currentPlayer; i++, j--) {
            count++;
        }
        if (count >= 5) return true;

        return false;
    }

    private void resetGame() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = 0;
            }
        }
        isBlackTurn = true;
        statusLabel.setText("轮到黑方落子");
        repaint();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GobangGame();
            }
        });
    }
}
